package 数组;

/**
 * @author tmh
 * @date 2024/9/10 22:08
 * @description
 */
public class T53最大子数组和 {

    //两个解法
    //1.同时更新sum和maxSum
    //2.使用dp来做
    public int maxSubArray(int[] nums) {
        int sum = 0;
        int maxSum = nums[0];
        for (int num : nums) {
            //更新sum，让加了当前数之后的sum和当前数比较
            sum=Math.max(sum+num,num);
            //更新maxSum
            maxSum=Math.max(maxSum,sum);
        }
        return maxSum;
    }


}
